3 * Tests for IP validity functions.
5 * Ported from /t/inc/IP.t by avar.
7 * @todo Test methods in this call should be split into a method and a
13 * @covers AvroValidator
15 class AvroValidatorTest
extends PHPUnit\Framework\TestCase
{
17 use MediaWikiCoversValidator
;
19 public function setUp() {
20 if ( !class_exists( 'AvroSchema' ) ) {
21 $this->markTestSkipped( 'Avro is required to run the AvroValidatorTest' );
26 public function getErrorsProvider() {
27 $stringSchema = AvroSchema
::parse( json_encode( [ 'type' => 'string' ] ) );
28 $stringArraySchema = AvroSchema
::parse( json_encode( [
32 $recordSchema = AvroSchema
::parse( json_encode( [
36 [ 'name' => 'id', 'type' => 'int', 'required' => true ],
39 $enumSchema = AvroSchema
::parse( json_encode( [
43 [ 'name' => 'count', 'type' => [ 'int', 'null' ] ],
49 'No errors with a simple string serialization',
50 $stringSchema, 'foobar', [],
54 'Cannot serialize integer into string',
55 $stringSchema, 5, 'Expected string, but recieved integer',
59 'Cannot serialize array into string',
60 $stringSchema, [], 'Expected string, but recieved array',
64 'allows and ignores extra fields',
65 $recordSchema, [ 'id' => 4, 'foo' => 'bar' ], [],
69 'detects missing fields',
70 $recordSchema, [], [ 'id' => 'Missing expected field' ],
74 'handles first element in enum',
75 $enumSchema, [ 'count' => 4 ], [],
79 'handles second element in enum',
80 $enumSchema, [ 'count' => null ], [],
84 'rejects element not in union',
85 $enumSchema, [ 'count' => 'invalid' ], [ 'count' => [
86 'Expected any one of these to be true',
88 'Expected integer, but recieved string',
89 'Expected null, but recieved string',
94 'Empty array is accepted',
95 $stringArraySchema, [], []
98 'correct array element accepted',
99 $stringArraySchema, [ 'fizzbuzz' ], []
102 'incorrect array element rejected',
103 $stringArraySchema, [ '12', 34 ], [ 'Expected string, but recieved integer' ]
109 * @dataProvider getErrorsProvider
111 public function testGetErrors( $message, $schema, $datum, $expected ) {
114 AvroValidator
::getErrors( $schema, $datum ),